//
//  Tabbar.m
//  微博2期
//
//  Created by J on 15/11/22.
//
//

#import "Tabbar.h"


@interface Tabbar ()


@property(nonatomic,weak) UIButton * button;


@end

@implementation Tabbar

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self =[super initWithFrame:frame]) {

        UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        button.size = button.currentBackgroundImage.size;
        [button addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
        self.button = button;
        [self addSubview:button];
    }
    
    return self;

}

//+点击
- (void)plusClick{
    
    if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
        
        
        [self.delegate tabBarDidClickPlusButton:self];
    }
    
}



//"<_UITabBarBackgroundView://tabbar的背景 是一个白色view
//"<UIButton: 0x7c05b800; fr
//"<UITabBarButton: 0x7c060b
//"<UITabBarButton: 0x7c0692
//"<UITabBarButton: 0x7c069e
//"<UITabBarButton: 0x7c06d3
//"<UIImageView: 0x7a058250;//0.5高度的黑色分割线


- (void)layoutSubviews{
    [super layoutSubviews];//必须调用  如果不调用super  就不会有 白色的背景View 和 分割线view

    //1.设置+按钮的位置
    
    self.button.centerX = self.width*0.5;
    self.button.centerY = self.height*0.5;

//    //2.设置其他tabbarButton的位置和尺寸
    CGFloat tabbarButtonW = self.width/5;
    CGFloat tabbarButtonIndex = 0;

    for (UIView * child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");//这个类敲不出来 只能通过字符串转换
                if ([child isKindOfClass:class]) {
//                    NSLog(@"%@",child.subviews);
                    //设置宽度
                    child.width = tabbarButtonW;
                    //设置x
                    child.x = tabbarButtonW * tabbarButtonIndex ;
                    tabbarButtonIndex++;//增加索引
                    if (tabbarButtonIndex==2) {
                        tabbarButtonIndex++;
                    }
                    
                }

    }
    
    
    
    
//    NSLog(@"%d",count);
//    for (int i=0; i<count; i++) {
//        UIView * child = self.subviews[i];
//        Class class = NSClassFromString(@"UITabBarButton");//这个类敲不出来 只能通过字符串转换
//        if ([child isKindOfClass:class]) {
//            //设置宽度
//            child.width = tabbarButtonW;
//            //设置x
//            child.x = tabbarButtonW * tabbarButtonIndex ;
//            
//            
//            tabbarButtonIndex++;//增加索引
//            if (tabbarButtonIndex==2) {
//                tabbarButtonIndex++;
//            }
//            
//        }
//    }
//    
//    JZJLog(@"%@",self.subviews);

    
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
